<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
use think\facade\Cache;

class Category extends Common
{
    protected $table = 'category';

    public function index()
    {
        $category = Db::name($this->table)->order('des desc,id asc')->select();
        $category = get_tree_option($category, 0);
        $this->assign('data', $category);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            $valdate = validate('Category');
            if (! $valdate->check($data)) {
                return ['err' => $valdate->getError()];
            }

            $id = $data['id'];
            unset($data['id']);
            if ($id) {
                if ($id < 10) {
                    // 默认菜单只能修改排序、图标、get变量
                    $data = ['des' => $data['des'], 'icon' => $data['icon'], 'get' => $data['get']];
                }
                $isSave = Db::name($this->table)->where('id', $id)->update($data);
            } else {
                $isSave = Db::name($this->table)->insert($data);
            }
            if (! $isSave) {
                return ['err' => '保存失败，请重试'];
            }
            // 成功保存时刷新菜单缓存
            Cache::clear();
            return ['ret' => '保存成功!'];
        }
        $category = Db::name($this->table)->order('des desc,id asc')->select();
        $category = get_tree_option($category, 0);
        $this->assign('data', $category);
        $data = $this->request->get();
        if (! empty($data['id'])) {
            $name = Db::name($this->table)->where('id',input('id'))->find();
            $this->assign('name', $name);
        }
        return $this->fetch();
    }

    // 删除菜单
    public function del()
    {
        if ($this->request->isAjax()) {
            $id = input('post.id/d');
            if (! $id) {
                return ['err' => '参数错误'];
            }

            if ($id < 10) {
                return ['err' => '系统固有菜单不能删除'];
            }

            $category = Db::name($this->table)->where('id', $id)->value('id');
            if (! $category) {
                return ['err' => '菜单不存在或被删除'];
            }

            $category = Db::name($this->table)->where('tid', $id)->count();
            if ($category) {
                return ['err' => '请先删除下级菜单'];
            }
            $isDel = Db::name($this->table)->delete($id);
            if (! $isDel) {
                return ['err' => '删除失败，请重试'];
            }
            return ['ret' => '删除成功'];
        }
        return ['err' => '请求方式有误'];
    }

    // 菜单显示隐藏切换
    public function hide()
    {
        if ($this->request->get()) {
            $id = input('get.id/d');
            $type = input('get.hide/d');
            if (empty($id) || empty($type)) {
                $this->error('缺少必要参数');
            }

            if ($id < 1) {
                $this->error('菜单不存在');
            }
            $field = 'hide';
            switch ($type) {
                case '1':
                    $field .= 'left';
                    break;
                case '2':
                    $field .= 'right';
                    break;
                default:
                    $field = '';
            }
            if (! $field) {
                $this->error('参数错误');
            }
            $category = Db::name($this->table)->where('id', $id)->find();
            if (empty($category)) {
                $this->error('菜单不存在或被删除');
            }
            $update = [$field => $category[$field] ? 0 : 1];
            $isUpdate = Db::name($this->table)->where('id', $id)->update($update);
            if (! $isUpdate) {
                $this->error('修改失败，请重试');
            }
            $this->redirect('category/index');
        }
    }
}
